/**
 *Created by liaoshengjun on 2017/9/7.
 */

BUI.use('common/page');
BUI.use([ 'common/search', 'bui/overlay', 'bui/grid', 'bui/data','bui/form','bui/select'],
    function(Search, Overlay, Grid, Data, Form,Select) {
        var privileges=sessionStorage.getItem('privileges');
        var privilegeList=privileges.split(',');
        var addPrivilege=false;
        if (privilegeList.length>0) {
            for (var i = 0; i < privilegeList.length; i++) {
                if (privilegeList[i] == 'saveUser') {
                    addPrivilege = true;
                    break;
                }
            }
        }
        var array=[];
        $.ajaxSettings.async = false;
        $.getJSON('showAllRoles',function (roles) {
            for(var i=0;i<roles.length;i++){
                array[i]={text:roles[i].desc,value:roles[i].roleId+''};
            }
        });
        $.ajaxSettings.async = true;
        var items = array,
            select = new Select.Select({
                render:'#roleNames',
                valueField:'#roleIds',
                multipleSelect:true,
                items:items
            });
        select.render();
        var statusEnum = {
            '0' : '作废',
            '1' : '有效'
        };
        // 查询表单
        var form = new Form.Form({
            srcNode : '#searchForm'
        }).render();
        // 定义表头
        columns = [
            {
                title : 'id',
                dataIndex : 'userId',
                visible : false
            },
            {
                title : '用户名',
                dataIndex : 'userName',
                elCls : 'center',
                width : 150
            },
            {
                title : '昵称',
                dataIndex : 'nickName',
                elCls : 'center',
                width : 200
            },
            {
                title : '联系手机',
                dataIndex : 'userPhone',
                elCls : 'center',
                width : 150
            },
            {
                title : '用户状态',
                dataIndex : 'userStatus',
                elCls : 'center',
                width : 100,
                renderer : BUI.Grid.Format
                    .enumRenderer(statusEnum)
            },
            {
                title : '角色',
                dataIndex : 'roleNames',
                elCls : 'center',
                width : 400
            },
            {
                title : '角色id',
                dataIndex : 'roleIds',
                visible : false
            }
        ];
        // 编辑
        var editing = new Grid.Plugins.DialogEditing({
                contentId : 'content',
                triggerCls : 'btn-edit',
                editor : {
                    title : '用户信息操作',
                    width : 600
                }
            }),
            // 获取数据
            store = new BUI.Data.Store({
                url : 'listUser',
                pageSize : 20,
                autoLoad : true,
                remoteSort : true
            });

        // 获取表格对象
        if(true){
            gridCfg = Search.createGridCfg(columns, {
                plugins : [ editing, Grid.Plugins.CheckSelection ],
                tbar : { // 添加
                    items : [ {
                        btnCls : 'button button-small',
                        text : '<i class="icon-plus"></i>添加',
                        listeners : {
                            'click' : addFunction
                        }
                    }, {
                        btnCls : 'button button-small',
                        text : '<i class="icon-refresh"></i>刷新',
                        listeners : {
                            'click' : refreshFunction
                        }
                    }]
                }
            });
        }else{
            gridCfg = Search.createGridCfg(columns, {
                plugins : [ editing, Grid.Plugins.CheckSelection ],
                tbar : {
                    items : [{
                        btnCls : 'button button-small',
                        text : '<i class="icon-refresh"></i>刷新',
                        listeners : {
                            'click' : refreshFunction
                        }
                    }]
                }
            });
        }
        // 刷新
        function refreshFunction() {
            search.load();
        }
        // 查询
        search = new Search({
            store : store,
            autoSearch : true,
            gridCfg : gridCfg
        });
        grid = search.get('grid');
        // 添加记录
        function addFunction() {
            var newData = {userName :'请填写用户名'};
            editing.add(newData);
        }
        // 编辑提交
        editing.on('accept', function(ev) {
            var record = editing.get('record');
            if (record.hasOwnProperty("b"))
                delete record["b"];
            if (record.hasOwnProperty(""))
                delete record[""];
            var saveUrl = record["userId"] === "" ? 'addUser'
                : 'updateUser';
            jQuery.ajax({
                type : 'POST',
                url : saveUrl,
                dataType : 'json',
                contentType : 'application/json;charset=UTF-8',
                data : JSON.stringify(record),
                complete : function(data) {
                    if (data.responseText == "success") { // 删除成功
                        search.load();
                    } else { // 删除失败
                        BUI.Message.Alert('保存失败！');
                    }
                }
            });
        });
    });